<!-- 合作审核 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="我的单位" prop="comId">
        <SelectCompany
          :defaultValue.sync="queryParams.comId"
          :companyType="0"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作单位" prop="myComId">
        <SelectCompany
          :defaultValue.sync="queryParams.myComId"
          :isAll="true"
          :companyType="0"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作项目" prop="apType">
        <el-select
          v-model="queryParams.apType"
          placeholder="请选择合作项目"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.cooperation_projects"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cooperation:audit:add']"
          >新增合作</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="teamList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="我的单位"
        align="center"
        prop="comName"
        min-width="250"
      />
      <el-table-column
        label="合作单位"
        align="center"
        prop="myComName"
        min-width="250"
      />
      <el-table-column label="申请合作项目" align="center" prop="apType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cooperation_projects"
            :value="scope.row.apType"
          />
        </template>
      </el-table-column>
      <el-table-column label="合作折扣" align="center" prop="discount">
        <template slot-scope="scope">
          <span>{{ scope.row.discount + "%" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="拒绝原因"
        align="center"
        prop="remark"
        min-width="200"
      />
      <el-table-column label="审核状态" align="center" prop="apStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cooperation_status" :value="scope.row.apStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.apStatus === '0'">
            <el-button
              v-hasPermi="['cooperation:audit:agree']"
              size="mini"
              type="text"
              @click="handleUpdates(scope.row)"
              >同意</el-button
            >
            <el-button
              v-hasPermi="['cooperation:audit:reject']"
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              >拒绝</el-button
            >
          </template>
          <template v-if="scope.row.apStatus === '2'">
            <el-button
              v-hasPermi="['cooperation:audit:reset']"
              size="mini"
              type="text"
              @click="reHandleUpdate(scope.row)"
              >重新申请</el-button
            >
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单位合作关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :show-message="false" label-width="140px">
        <el-form-item label="合作单位" prop="comId" required>
          <SelectCompany :defaultValue.sync="form.comId" :companyType="0" />
        </el-form-item>
        <el-form-item label="我的单位" prop="myComId" required>
          <SelectCompany :defaultValue.sync="form.myComId" :companyType="0" />
        </el-form-item>
        <el-form-item label="双方合作项目" prop="apType" required>
          <el-select v-model="form.apType" placeholder="请选择合作项目">
            <el-option
              v-for="dict in dict.type.cooperation_projects"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="设置折扣比例(%)"
          prop="discount"
          required
        >
          <el-input-number
            size="mini"
            :step="1"
            step-strictly
            v-model="form.discount"
            placeholder="请输入折扣比例"
            :min="0"
            :max="100"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title1"
      :visible.sync="open1"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="拒绝原因" prop="remark" required>
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入拒绝原因"
            v-model="form.remark"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTeam,
  getTeam,
  delTeam,
  addTeam,
  updateTeam,
  apTeam,
} from "@/api/cooperation/cooperation";
import SelectCompany from "@/components/SelectCompany";

export default {
  name: "Audit",
  dicts: ["cooperation_projects", "cooperation_status"],
  components: { SelectCompany },
  data() {
    return {
      // 遮罩层
      loading: true,
      createTime: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位合作关系表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      title1: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        comId: null,
        myComId: null,
        discount: null,
        apType: null,
        apTime: null,
        comArr: null,
        createTimeStart: null,
        createTimeEnd: null,
        searchValue: null,
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {

    /** 查询单位合作关系列表 */
    getList() {
      this.queryParams.createTimeStart = this.createTime && this.createTime.length ? this.createTime[0] : "";
      this.queryParams.createTimeEnd = this.createTime && this.createTime.length ? this.createTime[1] : "";
      this.loading = true;
      listTeam(this.queryParams).then((response) => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comId: null,
        myComId: null,
        discount: null,
        apType: null,
        apTime: null,
        remark: null,
        comArr: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位合作关系";
    },
    /** 修改按钮操作 */
    handleUpdates(row) {
      this.$confirm("确定要同意吗?", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (row.id != null) {
          apTeam(row.id).then((response) => {
            this.$modal.msgSuccess("同意成功");
            this.getList();
          });
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTeam(id).then((response) => {
        this.form = response.data;
        this.open1 = true;
        this.title1 = "拒绝原因";
      });
    },
    /** 修改按钮操作 */
    reHandleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTeam(id).then((response) => {
        this.form = response.data;
      });

      this.$confirm("确定要重新申请吗?", "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.form.remark = "";
        this.form.apStatus = "0";
        updateTeam(this.form).then((response) => {
          this.$modal.msgSuccess("申请成功");
          //this.open1 = false;
          this.getList();
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.apStatus = "2";
            updateTeam(this.form).then((response) => {
              this.$modal.msgSuccess("提交成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            if (this.form.comId === this.form.myComId) {
              this.$message({
                showClose: true,
                message: "不能选择相同单位作为合作单位",
                type: "error",
              });
              return;
            }
            // if(this.form.discount !==0){
            //   this.form.discount=this.form.discount/100;
            // }
            var oldArr = [this.form.comId, this.form.myComId];
            oldArr.sort((a, b) => {
              return a - b; // 升序
            });
            this.form.apStatus = "1";
            this.form.comArr = oldArr.join(",");
            addTeam(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除单位合作关系编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTeam(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  },
};
</script>
